package org.zjt.chapter1.http2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerUpgradeHandler;
import io.netty.handler.codec.http2.Http2CodecUtil;
import io.netty.handler.codec.http2.Http2FrameCodecBuilder;
import io.netty.handler.codec.http2.Http2ServerUpgradeCodec;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.AsciiString;
import io.netty.util.ReferenceCountUtil;
import org.zjt.chapter1.http2.helloworld.frame.server.HelloWorldHttp2Handler;
import org.zjt.chapter1.http2.helloworld.frame.server.Http2OrHttpHandler;
import org.zjt.chapter1.http2.helloworld.server.HelloWorldHttp1Handler;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-12-03 2:46 PM
 * @see
 */
public class Http2Server {

    public static void main(String[] args) throws Exception {
        EventLoopGroup boss = new NioEventLoopGroup(1);
        EventLoopGroup work = new NioEventLoopGroup(4);


        ServerBootstrap bootstrap = new ServerBootstrap();

        bootstrap.group(boss, work)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {

                        final ChannelPipeline p = ch.pipeline();
                        p.addLast(Http2FrameCodecBuilder.forServer().build());
                        p.addLast(new SimpleChannelInboundHandler<Object>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(msg.getClass() +"  " +msg);
                            }
                        });

                    }
                });

        ChannelFuture channelFuture = bootstrap.bind(8089).sync();

        channelFuture.channel().closeFuture().sync();

        boss.shutdownGracefully();
        work.shutdownGracefully();
    }
}
